package com.rn.gypsophila.netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;

/**
 * Description:
 *
 * @author 然诺
 * @date 2021/04/19
 */
public class MCClient {
    static final int BEGIN_PORT = 8000;
    static final int N_PORT = 100;
    private static final String SERVER_HOST = "127.0.0.1";
//    private static final String SERVER_HOST = "192.168.125.102";

    public static void main(String[] args) {
        new MCClient().start(BEGIN_PORT, N_PORT);
    }

    public void start(final int beginPort, int nPort) {
        EventLoopGroup group = new NioEventLoopGroup();

        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.SO_KEEPALIVE, true)
                    .option(ChannelOption.SO_REUSEADDR, true)
                    // 缓冲区的大小建议设置为消息的平均大小临近的2的次幂
                    .option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(64, 1024, 65536))
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) {
                            ch.pipeline()
                                    .addLast(new LineBasedFrameDecoder(1024))
                                    .addLast(new StringDecoder())
                                    .addLast(new StringEncoder())
                                    // 建议180s
                                    .addLast(new IdleStateHandler(0, 10, 0))
                                    .addLast(new HeartbeatHandler())
                                    .addLast();
                        }
                    });
            ChannelFuture channelFuture = bootstrap.connect(SERVER_HOST, beginPort);
            channelFuture.addListener((ChannelFutureListener) future -> {
                if (!future.isSuccess()) {
                    System.out.println("connect failed, exit!");
                    System.exit(0);
                }
            });
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            group.shutdownGracefully();
        }


//        int index = 0;
//        int port;
//        while (!Thread.interrupted()) {
//            port = beginPort + index;
//            try {
//                ChannelFuture channelFuture = bootstrap.connect(SERVER_HOST, port);
//                channelFuture.addListener((ChannelFutureListener) future -> {
//                    if (!future.isSuccess()) {
//                        System.out.println("connect failed, exit!");
//                        System.exit(0);
//                    }
//                });
//                channelFuture.get();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//
//            if (++index == nPort) {
//                index = 0;
//            }
//        }
    }

}